package com.sxhuayuan.parking.controller.member;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sxhuayuan.parking.compenent.Message;
import com.sxhuayuan.parking.compenent.web.RespMessage;
import com.sxhuayuan.parking.dto.PartnerpayQueryDTO;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.plugin.partnerpay.PartnerpayApiService;
import com.sxhuayuan.parking.plugin.partnerpay.result.JumpToSignPageResult;
import com.sxhuayuan.parking.plugin.partnerpay.result.QueryStateResult;
import com.sxhuayuan.parking.service.MemberService;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.PartnerpayService;
import com.sxhuayuan.parking.service.PluginService;

@Controller("memberPartnerpayController")
@RequestMapping("/member/partnerpay")
public class PartnerpayController extends BaseController {

	@Resource(name = "partnerpayServiceImpl")
	PartnerpayService service;
	@Resource(name = "memberServiceImpl")
	MemberService memberService;
	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService lotService;
	@Resource(name = "pluginServiceImpl")
	PluginService pluginService;

	/**
	 * 签约列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/state", method = RequestMethod.GET)
	public String state() {

		return "/member/partnerpay/state";
	}

	/**
	 * 查询车主服务状态
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/state", method = RequestMethod.POST)
	public @ResponseBody RespMessage state(@Validated @RequestBody PartnerpayQueryDTO dto) {
		PartnerpayApiService apiService = pluginService.getPartnerpayApiService(dto.getPlatform());
		if (apiService == null) {
			return RespMessage.error(Message.PARTNERPAY_PLATFORM_IS_NOT_SUPPORT);
		}
		ParkingLot lot = lotService.findBySn(dto.getParkingLotSn());
		if (lot == null) {
			return RespMessage.error(Message.PARKING_LOT_NOT_EXISTS);
		}
		int loop = 0;
		QueryStateResult queryStateResult = null;
		while (queryStateResult == null && loop++ < 3) {
			queryStateResult = apiService.queryUserState(lot, dto.getOpenid(), dto.getSubOpenid(), dto.getCarPlate(), dto.getClient());
		}
		if (queryStateResult == null) {
			return RespMessage.error(Message.COMMON_NETWORK_ERR);
		}
		if (queryStateResult.getIsResultSuccess()) {
			JSONObject data = new JSONObject();
			// 用户状态
			data.put("userState", queryStateResult.isUserStateNormal());
			// 车牌列表
			data.put("carPlates", queryStateResult.getCarPlates());
			if (queryStateResult.isNeedToSign()) {
				String path = queryStateResult.getPath();
				JumpToSignPageResult toSignPageResult = apiService.jumpToSignPage(dto.getTradeScene(), lot, path, dto.getOpenid(), dto.getSubOpenid(), dto.getCarPlate(),
						dto.getChannelType(), dto.getClient(), dto.getClient());
				data.put("redirect", toSignPageResult);
			}
			return RespMessage.success(data);
		}
		return RespMessage.error(queryStateResult.getMsg());
	}

	/**
	 * 未签约车主服务时的前端跳转参数
	 * 
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/jump", method = RequestMethod.POST)
	public @ResponseBody RespMessage jump(@Validated @RequestBody PartnerpayQueryDTO dto) {
		PartnerpayApiService apiService = pluginService.getPartnerpayApiService(dto.getPlatform());
		if (apiService == null) {
			return RespMessage.error(Message.PARTNERPAY_PLATFORM_IS_NOT_SUPPORT);
		}
		// ParkingLot lot = lotService.findBySn(dto.getParkingLotSn());
		ParkingLot lot = lotService.findBySn(getLotSnFromSession());
		if (lot == null) {
			return RespMessage.error(Message.PARKING_LOT_NOT_EXISTS);
		}

		int loop = 0;
		QueryStateResult queryStateResult = null;
		while (queryStateResult == null && loop++ < 3) {
			queryStateResult = apiService.queryUserState(lot, null, null, null, dto.getClient());
		}
		if (queryStateResult == null) {
			return RespMessage.error(Message.COMMON_NETWORK_ERR);
		}
		if (queryStateResult.isNeedToSign()) {
			String path = queryStateResult.getPath();
			JumpToSignPageResult toSignPageResult = apiService.jumpToSignPage(dto.getTradeScene(), lot, path, dto.getOpenid(), dto.getSubOpenid(), dto.getCarPlate(), dto.getChannelType(),
					dto.getClient(), dto.getClient());
			return RespMessage.success(toSignPageResult);
		} else {
			return RespMessage.error("无须签约");
		}
	}

}
